package fr.paris.sud.com;


import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JToolBar;
import javax.swing.SwingWorker;
import fr.lri.swingstates.canvas.CImage;
import fr.lri.swingstates.canvas.CShape;
import fr.lri.swingstates.canvas.Canvas;

public class IconThumbnail extends JFrame {

	//public Canvas canvas = new Canvas(10,100);
	public JLabel photographLabel = new JLabel();
	public JToolBar buttonBar = new JToolBar();

	public ImageStructure iImg;
	private MissingIcon placeholderIcon = new MissingIcon();
	
	//ImageStructure iMap;
	
	
	
	public CImage loadimages(ImageStructure iMap, int xPosition, int yPosition){   


		ImageIcon thumb = new ImageIcon(iMap.getImagePath());

		BufferedImage scaledImg;
		scaledImg = getScaledImage(thumb.getImage(),128,128);
		
		ByteArrayOutputStream imageOutputStream = new ByteArrayOutputStream();
		//File outputfile = new File("thumb.png");

		try {
			ImageIO.write(scaledImg, "png", imageOutputStream);  

		} catch (IOException e) {
			e.printStackTrace();  
		}

		byte[] byteImage = imageOutputStream.toByteArray(); 
		ByteArrayInputStream imageInputStream = new ByteArrayInputStream(byteImage);
		CImage icon = PhotoManagerWindow.canvas.newImage(xPosition-64, yPosition-64, imageInputStream);
		icon.addTag("draggable");
		return icon;
	}

	public static InputStream loadWholeImageToDisplay(String path){

		double percent=0.2;
		int scaledWidth=0;
		int scaledHeight=0;
		
		ImageIcon img = new ImageIcon(path);
		

		ByteArrayOutputStream imageOutputStream = new ByteArrayOutputStream();
		//File outputfile = new File("thumb.png");
		
		PhotoManagerWindow.imageViewer.removeAllShapes();
		
		File inputFile = new File(path);
		try {
			BufferedImage inputImage = ImageIO.read(inputFile);
			scaledWidth = (int) (inputImage.getWidth() * percent);
	        scaledHeight = (int) (inputImage.getHeight() * percent);
	        
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		BufferedImage scaledImg;
		scaledImg = getScaledImage(img.getImage(), scaledWidth, scaledHeight);
		
		try {
			ImageIO.write(scaledImg, "png", imageOutputStream);  

		} catch (IOException e) {
			e.printStackTrace();  
		}

		byte[] byteImage = imageOutputStream.toByteArray(); 
		ByteArrayInputStream imageInputStream = new ByteArrayInputStream(byteImage);
		
		return imageInputStream;

	}



	private static BufferedImage getScaledImage(Image srcImg, int w, int h){
		
		BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2 = resizedImg.createGraphics();
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2.drawImage(srcImg, 0, 0, w, h, null);
		g2.dispose();
		return resizedImg;
	}
	
}
